home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / readline-2.0mg.tar.gz / readline-2.0mg.tar / readline-2.0mg / complete.c < prev    next >
C/C++ Source or Header  |  1994-11-20  |  38KB  |  1,422 lines

  1. /* complete.c -- filename completion for readline. */
  2.  
  3. /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  4.  
  5.    This file is part of the GNU Readline Library, a library for
  6.    reading lines of text with interactive input and history editing.
  7.  
  8.    The GNU Readline Library is free software; you can redistribute it
  9.    and/or modify it under the terms of the GNU General Public License
  10.    as published by the Free Software Foundation; either version 1, or
  11.    (at your option) any later version.
  12.  
  13.    The GNU Readline Library is distributed in the hope that it will be
  14.    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15.    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    The GNU General Public License is often shipped with GNU software, and
  19.    is generally kept in a file called COPYING or LICENSE.  If you do not
  20.    have a copy of the license, write to the Free Software Foundation,
  21.    675 Mass Ave, Cambridge, MA 02139, USA. */
  22. #define READLINE_LIBRARY
  23.  
  24. #if defined (HAVE_CONFIG_H)
  25. #  include "config.h"
  26. #endif
  27.  
  28. #include <stdio.h>
  29. #include <sys/types.h>
  30. #include <fcntl.h>
  31. #if !defined (NO_SYS_FILE)
  32. #  include <sys/file.h>
  33. #endif /* !NO_SYS_FILE */
  34.  
  35. #if defined (HAVE_UNISTD_H)
  36. #  include <unistd.h>
  37. #endif /* HAVE_UNISTD_H */
  38.  
  39. #if defined (HAVE_STDLIB_H)
  40. #  include <stdlib.h>
  41. #else
  42. #  include "ansi_stdlib.h"
  43. #endif /* HAVE_STDLIB_H */
  44.  
  45. #include <errno.h>
  46. /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
  47. #if !defined (errno)
  48. extern int errno;
  49. #endif /* !errno */
  50.  
  51. #include <pwd.h>
  52. #if defined (USG) && !defined (HAVE_GETPW_DECLS)
  53. extern struct passwd *getpwent ();
  54. #endif /* USG && !HAVE_GETPW_DECLS */
  55.  
  56. /* ISC systems don't define getpwent() if _POSIX_SOURCE is defined. */
  57. #if defined (isc386) && defined (_POSIX_SOURCE)
  58. #  if defined (__STDC__)
  59. extern struct passwd *getpwent (void);
  60. #  else
  61. extern struct passwd *getpwent ();
  62. #  endif /* !__STDC__ */
  63. #endif /* isc386 && _POSIX_SOURCE */
  64.  
  65. #include "posixstat.h"
  66.  
  67. /* System-specific feature definitions and include files. */
  68. #include "rldefs.h"
  69.  
  70. /* Some standard library routines. */
  71. #include "readline.h"
  72.  
  73. /* Possible values for do_replace in rl_complete_internal. */
  74. #define NO_MATCH    0
  75. #define SINGLE_MATCH    1
  76. #define MULT_MATCH    2
  77.  
  78. #if !defined (strchr) && !defined (__STDC__)
  79. extern char *strchr (), *strrchr ();
  80. #endif /* !strchr && !__STDC__ */
  81.  
  82. extern char *tilde_expand ();
  83. extern char *rl_copy_text ();
  84.  
  85. extern Function *rl_last_func;
  86. extern int rl_editing_mode;
  87. extern int screenwidth;
  88.  
  89. /* Forward declarations for functions defined and used in this file. */
  90. char *filename_completion_function ();
  91. char **completion_matches ();
  92.  
  93. static int compare_strings ();
  94. static char *rl_strpbrk ();
  95.  
  96. #if defined (STATIC_MALLOC)
  97. static char *xmalloc (), *xrealloc ();
  98. #else
  99. extern char *xmalloc (), *xrealloc ();
  100. #endif /* STATIC_MALLOC */
  101.  
  102. /* If non-zero, then this is the address of a function to call when
  103.    completing on a directory name.  The function is called with
  104.    the address of a string (the current directory name) as an arg. */
  105. Function *rl_directory_completion_hook = (Function *)NULL;
  106.  
  107. /* Non-zero means readline completion functions perform tilde expansion. */
  108. int rl_complete_with_tilde_expansion = 0;
  109.  
  110. /* If non-zero, non-unique completions always show the list of matches. */
  111. int _rl_complete_show_all = 0;
  112.  
  113. #if defined (VISIBLE_STATS)
  114. #  if !defined (X_OK)
  115. #    define X_OK 1
  116. #  endif
  117.  
  118. static int stat_char ();
  119.  
  120. /* Non-zero means add an additional character to each filename displayed
  121.    during listing completion iff rl_filename_completion_desired which helps
  122.    to indicate the type of file being listed. */
  123. int rl_visible_stats = 0;
  124. #endif /* VISIBLE_STATS */
  125.  
  126. /* **************************************************************** */
  127. /*                                    */
  128. /*    Completion matching, from readline's point of view.        */
  129. /*                                    */
  130. /* **************************************************************** */
  131.  
  132. /* Pointer to the generator function for completion_matches ().
  133.    NULL means to use filename_entry_function (), the default filename
  134.    completer. */
  135. Function *rl_completion_entry_function = (Function *)NULL;
  136.  
  137. /* Pointer to alternative function to create matches.
  138.    Function is called with TEXT, START, and END.
  139.    START and END are indices in RL_LINE_BUFFER saying what the boundaries
  140.    of TEXT are.
  141.    If this function exists and returns NULL then call the value of
  142.    rl_completion_entry_function to try to match, otherwise use the
  143.    array of strings returned. */
  144. CPPFunction *rl_attempted_completion_function = (CPPFunction *)NULL;
  145.  
  146. /* Non-zero means to suppress normal filename completion after the
  147.    user-specified completion function has been called. */
  148. int rl_attempted_completion_over = 0;
  149.  
  150. /* Local variable states what happened during the last completion attempt. */
  151. static int completion_changed_buffer = 0;
  152.  
  153. /* Complete the word at or before point.  You have supplied the function
  154.    that does the initial simple matching selection algorithm (see
  155.    completion_matches ()).  The default is to do filename completion. */
  156.  
  157. rl_complete (ignore, invoking_key)
  158.      int ignore, invoking_key;
  159. {
  160.   if (rl_last_func == rl_complete && !completion_changed_buffer)
  161.     return (rl_complete_internal ('?'));
  162.   else if (_rl_complete_show_all)
  163.     return (rl_complete_internal ('!'));
  164.   else
  165.     return (rl_complete_internal (TAB));
  166. }
  167.  
  168. /* List the possible completions.  See description of rl_complete (). */
  169. rl_possible_completions (ignore, invoking_key)
  170.      int ignore, invoking_key;
  171. {
  172.   return (rl_complete_internal ('?'));
  173. }
  174.  
  175. rl_insert_completions (ignore, invoking_key)
  176.      int ignore, invoking_key;
  177. {
  178.   return (rl_complete_internal ('*'));
  179. }
  180.  
  181. /* The user must press "y" or "n". Non-zero return means "y" pressed. */
  182. get_y_or_n ()
  183. {
  184.   int c;
  185.  
  186.   for (;;)
  187.     {
  188.       c = rl_read_key ();
  189.       if (c == 'y' || c == 'Y' || c == ' ')
  190.     return (1);
  191.       if (c == 'n' || c == 'N' || c == RUBOUT)
  192.     return (0);
  193.       if (c == ABORT_CHAR)
  194.     rl_abort ();
  195.       ding ();
  196.     }
  197. }
  198.  
  199. /* Up to this many items will be displayed in response to a
  200.    possible-completions call.  After that, we ask the user if
  201.    she is sure she wants to see them all. */
  202. int rl_completion_query_items = 100;
  203.  
  204. /* The basic list of characters that signal a break between words for the
  205.    completer routine.  The contents of this variable is what breaks words
  206.    in the shell, i.e. " \t\n\"\\'`@$><=" */
  207. char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(";
  208.  
  209. /* The list of characters that signal a break between words for
  210.    rl_complete_internal.  The default list is the contents of
  211.    rl_basic_word_break_characters.  */
  212. char *rl_completer_word_break_characters = (char *)NULL;
  213.  
  214. /* List of characters which can be used to quote a substring of the line.
  215.    Completion occurs on the entire substring, and within the substring
  216.    rl_completer_word_break_characters are treated as any other character,
  217.    unless they also appear within this list. */
  218. char *rl_completer_quote_characters = (char *)NULL;
  219.  
  220. /* List of characters that are word break characters, but should be left
  221.    in TEXT when it is passed to the completion function.  The shell uses
  222.    this to help determine what kind of completing to do. */
  223. char *rl_special_prefixes = (char *)NULL;
  224.  
  225. /* If non-zero, then disallow duplicates in the matches. */
  226. int rl_ignore_completion_duplicates = 1;
  227.  
  228. /* Non-zero means that the results of the matches are to be treated
  229.    as filenames.  This is ALWAYS zero on entry, and can only be changed
  230.    within a completion entry finder function. */
  231. int rl_filename_completion_desired = 0;
  232.  
  233. /* Non-zero means that the results of the matches are to be quoted using
  234.    double quotes (or an application-specific quoting mechanism) if the
  235.    filename contains any characters in rl_word_break_chars.  This is
  236.    ALWAYS non-zero on entry, and can only be changed within a completion
  237.    entry finder function. */
  238. int rl_filename_quoting_desired = 1;
  239.  
  240. /* This function, if defined, is called by the completer when real
  241.    filename completion is done, after all the matching names have been
  242.    generated. It is passed a (char**) known as matches in the code below.
  243.    It consists of a NULL-terminated array of pointers to potential
  244.    matching strings.  The 1st element (matches[0]) is the maximal
  245.    substring that is common to all matches. This function can re-arrange
  246.    the list of matches as required, but all elements of the array must be
  247.    free()'d if they are deleted. The main intent of this function is
  248.    to implement FIGNORE a la SunOS csh. */
  249. Function *rl_ignore_some_completions_function = (Function *)NULL;
  250.  
  251. #if defined (SHELL)
  252. /* A function to strip quotes that are not protected by backquotes.  It
  253.    allows single quotes to appear within double quotes, and vice versa.
  254.    It should be smarter.  It's fairly shell-specific, hence the SHELL
  255.    definition wrapper. */
  256. static char *
  257. _delete_quotes (text)
  258.      char *text;
  259. {
  260.   char *ret, *p, *r;
  261.   int l, quoted;
  262.  
  263.   l = strlen (text);
  264.   ret = xmalloc (l + 1);
  265.   for (quoted = 0, p = text, r = ret; p && *p; p++)
  266.     {
  267.       /* Allow backslash-quoted characters to pass through unscathed. */
  268.       if (*p == '\\')
  269.         continue;
  270.       /* Close quote. */
  271.       if (quoted && *p == quoted)
  272.     {
  273.       quoted = 0;
  274.       continue;
  275.     }
  276.       /* Open quote. */
  277.       if (quoted == 0 && (*p == '\'' || *p == '"'))
  278.     {
  279.       quoted = *p;
  280.       continue;
  281.     }
  282.       *r++ = *p;
  283.     }
  284.   *r = '\0';
  285.   return ret;
  286. }
  287. #endif /* SHELL */
  288.  
  289. /* Return the portion of PATHNAME that should be output when listing
  290.    possible completions.  If we are hacking filename completion, we
  291.    are only interested in the basename, the portion following the
  292.    final slash.  Otherwise, we return what we were passed. */
  293. static char *
  294. printable_part (pathname)
  295.       char *pathname;
  296. {
  297.   char *temp = (char *)NULL;
  298.  
  299.   if (rl_filename_completion_desired)
  300.     temp = strrchr (pathname, '/');
  301.  
  302.   if (!temp)
  303.     return (pathname);
  304.   else
  305.     return (++temp);
  306. }
  307.  
  308. /* Output TO_PRINT to rl_outstream.  If VISIBLE_STATS is defined and we
  309.    are using it, check for and output a single character for `special'
  310.    filenames.  Return 1 if we printed an extension character, 0 if not. */
  311. static int
  312. print_filename (to_print, full_pathname)
  313.      char *to_print, *full_pathname;
  314. {
  315. #if !defined (VISIBLE_STATS)
  316.   fputs (to_print, rl_outstream);
  317.   return 0;
  318. #else  
  319.   char *s, c, *new_full_pathname;
  320.   int extension_char = 0, slen, tlen;
  321.  
  322.   fputs (to_print, rl_outstream);
  323.   if (rl_filename_completion_desired && rl_visible_stats)
  324.     {
  325.       /* If to_print != full_pathname, to_print is the basename of the
  326.      path passed.  In this case, we try to expand the directory
  327.      name before checking for the stat character. */
  328.       if (to_print != full_pathname)
  329.     {
  330.       /* Terminate the directory name. */
  331.       c = to_print[-1];
  332.       to_print[-1] = '\0';
  333.  
  334.       s = tilde_expand (full_pathname);
  335.       if (rl_directory_completion_hook)
  336.         (*rl_directory_completion_hook) (&s);
  337.  
  338.       slen = strlen (s);
  339.       tlen = strlen (to_print);
  340.       new_full_pathname = xmalloc (slen + tlen + 2);
  341.       strcpy (new_full_pathname, s);
  342.       new_full_pathname[slen] = '/';
  343.       strcpy (new_full_pathname + slen + 1, to_print);
  344.  
  345.       extension_char = stat_char (new_full_pathname);
  346.  
  347.       free (new_full_pathname);
  348.       to_print[-1] = c;
  349.     }
  350.       else
  351.     {
  352.       s = tilde_expand (full_pathname);
  353.       extension_char = stat_char (s);
  354.     }
  355.  
  356.       free (s);
  357.       if (extension_char)
  358.     putc (extension_char, rl_outstream);
  359.       return (extension_char != 0);
  360.     }
  361.   else
  362.     return 0;
  363. #endif /* VISIBLE_STATS */
  364. }
  365.  
  366. /* Complete the word at or before point.
  367.    WHAT_TO_DO says what to do with the completion.
  368.    `?' means list the possible completions.
  369.    TAB means do standard completion.
  370.    `*' means insert all of the possible completions.
  371.    `!' means to do standard completion, and list all possible completions if
  372.    there is more than one. */
  373. rl_complete_internal (what_to_do)
  374.      int what_to_do;
  375. {
  376.   char **matches;
  377.   Function *our_func;
  378.   int start, scan, end, delimiter = 0, pass_next;
  379.   char *text, *saved_line_buffer;
  380.   char *replacement;
  381.   char quote_char = '\0';
  382.   int found_quote = 0;
  383.  
  384.   if (rl_line_buffer)
  385.     saved_line_buffer = savestring (rl_line_buffer);
  386.   else
  387.     saved_line_buffer = (char *)NULL;
  388.  
  389.   if (rl_completion_entry_function)
  390.     our_func = rl_completion_entry_function;
  391.   else
  392.     our_func = (Function *)filename_completion_function;
  393.  
  394.   /* Only the completion entry function can change these. */
  395.   rl_filename_completion_desired = 0;
  396.   rl_filename_quoting_desired = 1;
  397.  
  398.   /* We now look backwards for the start of a filename/variable word. */
  399.   end = rl_point;
  400.  
  401.   if (rl_point)
  402.     {
  403.       if (rl_completer_quote_characters)
  404.     {
  405.       /* We have a list of characters which can be used in pairs to
  406.          quote substrings for the completer.  Try to find the start
  407.          of an unclosed quoted substring. */
  408.       /* FOUND_QUOTE is set so we know what kind of quotes we found. */
  409.       for (scan = pass_next = 0; scan < end; scan++)
  410.         {
  411.           if (pass_next)
  412.         {
  413.           pass_next = 0;
  414.           continue;
  415.         }
  416.  
  417.           if (rl_line_buffer[scan] == '\\')
  418.         {
  419.           pass_next = 1;
  420.           found_quote |= 4;
  421.           continue;
  422.         }
  423.  
  424.           if (quote_char != '\0')
  425.         {
  426.           /* Ignore everything until the matching close quote char. */
  427.           if (rl_line_buffer[scan] == quote_char)
  428.             {
  429.               /* Found matching close.  Abandon this substring. */
  430.               quote_char = '\0';
  431.               rl_point = end;
  432.             }
  433.         }
  434.           else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan]))
  435.         {
  436.           /* Found start of a quoted substring. */
  437.           quote_char = rl_line_buffer[scan];
  438.           rl_point = scan + 1;
  439.           /* Shell-like quoting conventions. */
  440.           if (quote_char == '\'')
  441.             found_quote |= 1;
  442.           else if (quote_char == '"')
  443.             found_quote |= 2;
  444.         }
  445.         }
  446.     }
  447.  
  448.       if (rl_point == end)
  449.     {
  450.       int quoted = 0;
  451.       /* We didn't find an unclosed quoted substring upon which to do
  452.          completion, so use the word break characters to find the
  453.          substring on which to complete. */
  454.       while (--rl_point)
  455.         {
  456.           scan = rl_line_buffer[rl_point];
  457.  
  458.           if (strchr (rl_completer_word_break_characters, scan) == 0)
  459.         continue;
  460.  
  461. #if defined (SHELL)
  462.           /* Don't let word break characters in quoted substrings break
  463.          words for the completer. */
  464.           if (found_quote && char_is_quoted (rl_line_buffer, rl_point))
  465.         continue;
  466. #endif /* SHELL */
  467.  
  468.           /* Convoluted code, but it avoids an n^2 algorithm with calls
  469.                to char_is_quoted. */
  470.           break;
  471.         }
  472.     }
  473.  
  474.       /* If we are at an unquoted word break, then advance past it. */
  475.       scan = rl_line_buffer[rl_point];
  476. #if defined (SHELL)
  477.       if ((found_quote == 0 || char_is_quoted (rl_line_buffer, rl_point) == 0) &&
  478.           strchr (rl_completer_word_break_characters, scan))
  479. #else
  480.       if (strchr (rl_completer_word_break_characters, scan))
  481. #endif
  482.     {
  483.       /* If the character that caused the word break was a quoting
  484.          character, then remember it as the delimiter. */
  485.       if (strchr ("\"'", scan) && (end - rl_point) > 1)
  486.         delimiter = scan;
  487.  
  488.       /* If the character isn't needed to determine something special
  489.          about what kind of completion to perform, then advance past it. */
  490.       if (!rl_special_prefixes || strchr (rl_special_prefixes, scan) == 0)
  491.         rl_point++;
  492.     }
  493.     }
  494.  
  495.   /* At this point, we know we have an open quote if quote_char != '\0'. */
  496.   start = rl_point;
  497.   rl_point = end;
  498.   text = rl_copy_text (start, end);
  499.  
  500.   /* If the user wants to TRY to complete, but then wants to give
  501.      up and use the default completion function, they set the
  502.      variable rl_attempted_completion_function. */
  503.   if (rl_attempted_completion_function)
  504.     {
  505.       matches = (*rl_attempted_completion_function) (text, start, end);
  506.  
  507.       if (matches || rl_attempted_completion_over)
  508.     {
  509.       rl_attempted_completion_over = 0;
  510.       our_func = (Function *)NULL;
  511.       goto after_usual_completion;
  512.     }
  513.     }
  514.  
  515. #if defined (SHELL)
  516.   /* Beware -- we're stripping the quotes here.  Do this only if we know
  517.      we are doing filename completion. */
  518.   if (found_quote && our_func == (Function *)filename_completion_function)
  519.     {
  520.       /* delete single and double quotes */
  521.       replacement = _delete_quotes (text);
  522.       free (text);
  523.       text = replacement;
  524.       replacement = (char *)0;
  525.     }
  526. #endif /* SHELL */
  527.  
  528.   matches = completion_matches (text, our_func);
  529.  
  530.  after_usual_completion:
  531.   free (text);
  532.  
  533.   if (!matches)
  534.     ding ();
  535.   else
  536.     {
  537.       register int i;
  538.       int should_quote;
  539.  
  540.       /* It seems to me that in all the cases we handle we would like
  541.      to ignore duplicate possiblilities.  Scan for the text to
  542.      insert being identical to the other completions. */
  543.       if (rl_ignore_completion_duplicates)
  544.     {
  545.       char *lowest_common;
  546.       int j, newlen = 0;
  547.       char dead_slot;
  548.       char **temp_array;
  549.  
  550.       /* Sort the items. */
  551.       /* It is safe to sort this array, because the lowest common
  552.          denominator found in matches[0] will remain in place. */
  553.       for (i = 0; matches[i]; i++);
  554.       qsort (matches, i, sizeof (char *), compare_strings);
  555.  
  556.       /* Remember the lowest common denominator for it may be unique. */
  557.       lowest_common = savestring (matches[0]);
  558.  
  559.       for (i = 0; matches[i + 1]; i++)
  560.         {
  561.           if (strcmp (matches[i], matches[i + 1]) == 0)
  562.         {
  563.           free (matches[i]);
  564.           matches[i] = (char *)&dead_slot;
  565.         }
  566.           else
  567.         newlen++;
  568.         }
  569.  
  570.       /* We have marked all the dead slots with (char *)&dead_slot.
  571.          Copy all the non-dead entries into a new array. */
  572.       temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *));
  573.       for (i = j = 1; matches[i]; i++)
  574.         {
  575.           if (matches[i] != (char *)&dead_slot)
  576.         temp_array[j++] = matches[i];
  577.         }
  578.       temp_array[j] = (char *)NULL;
  579.  
  580.       if (matches[0] != (char *)&dead_slot)
  581.         free (matches[0]);
  582.       free (matches);
  583.  
  584.       matches = temp_array;
  585.  
  586.       /* Place the lowest common denominator back in [0]. */
  587.       matches[0] = lowest_common;
  588.  
  589.       /* If there is one string left, and it is identical to the
  590.          lowest common denominator, then the LCD is the string to
  591.          insert. */
  592.       if (j == 2 && strcmp (matches[0], matches[1]) == 0)
  593.         {
  594.           free (matches[1]);
  595.           matches[1] = (char *)NULL;
  596.         }
  597.     }
  598.  
  599.       switch (what_to_do)
  600.     {
  601.     case TAB:
  602.     case '!':
  603.       /* If we are matching filenames, then here is our chance to
  604.          do clever processing by re-examining the list.  Call the
  605.          ignore function with the array as a parameter.  It can
  606.          munge the array, deleting matches as it desires. */
  607.       if (rl_ignore_some_completions_function &&
  608.           our_func == (Function *)filename_completion_function)
  609.         (void)(*rl_ignore_some_completions_function)(matches);
  610.  
  611.       /* If we are doing completion on quoted substrings, and any matches
  612.          contain any of the completer_word_break_characters, then auto-
  613.          matically prepend the substring with a quote character (just pick
  614.          the first one from the list of such) if it does not already begin
  615.          with a quote string.  FIXME: Need to remove any such automatically
  616.          inserted quote character when it no longer is necessary, such as
  617.          if we change the string we are completing on and the new set of
  618.          matches don't require a quoted substring. */
  619.       replacement = matches[0];
  620.  
  621.       should_quote = matches[0] && rl_completer_quote_characters &&
  622.              rl_filename_completion_desired &&
  623.              rl_filename_quoting_desired;
  624.  
  625.       if (should_quote)
  626. #if defined (SHELL)
  627.         should_quote = should_quote && (!quote_char || quote_char == '"');
  628. #else
  629.         should_quote = should_quote && !quote_char;
  630. #endif
  631.  
  632.       if (should_quote)
  633.         {
  634.           int do_replace;
  635.  
  636.           do_replace = NO_MATCH;
  637.  
  638.           /* If there is a single match, see if we need to quote it.
  639.          This also checks whether the common prefix of several
  640.          matches needs to be quoted.  If the common prefix should
  641.          not be checked, add !matches[1] to the if clause. */
  642.           should_quote = rl_strpbrk (matches[0], rl_completer_word_break_characters) != 0;
  643. #if defined (SHELL)
  644.           should_quote = should_quote || rl_strpbrk (matches[0], "#$`") != 0;
  645. #endif
  646.  
  647.           if (should_quote)
  648.         do_replace = matches[1] ? MULT_MATCH : SINGLE_MATCH;
  649.  
  650.           if (do_replace != NO_MATCH)
  651.         {
  652. #if defined (SHELL)
  653.           /* Quote the replacement, since we found an
  654.              embedded word break character in a potential
  655.              match. */
  656.           char *rtext, *mtext;
  657.           int rlen;
  658.           extern char *double_quote ();    /* in builtins/common.c */
  659.  
  660.           /* If DO_REPLACE == MULT_MATCH, it means that there is
  661.              more than one match.  In this case, we do not add
  662.              the closing quote or attempt to perform tilde
  663.              expansion.  If DO_REPLACE == SINGLE_MATCH, we try
  664.              to perform tilde expansion, because double quotes
  665.              inhibit tilde expansion by the shell. */
  666.  
  667.           mtext = matches[0];
  668.           if (mtext[0] == '~' && do_replace == SINGLE_MATCH)
  669.             mtext = tilde_expand (matches[0]);
  670.           rtext = double_quote (mtext);
  671.           if (mtext != matches[0])
  672.             free (mtext);
  673.  
  674.           rlen = strlen (rtext);
  675.           replacement = xmalloc (rlen + 1);
  676.           /* If we're completing on a quoted string where the user
  677.              has already supplied the opening quote, we don't want
  678.              the quote in the replacement text, and we reset
  679.              QUOTE_CHAR to 0 to avoid an extra closing quote. */
  680.           if (quote_char == '"')
  681.             {
  682.               strcpy (replacement, rtext + 1);
  683.               rlen--;
  684.               quote_char = 0;
  685.             }
  686.           else
  687.             strcpy (replacement, rtext);
  688.           if (do_replace == MULT_MATCH)
  689.             replacement[rlen - 1] = '\0';
  690.           free (rtext);
  691. #else /* !SHELL */
  692.           /* Found an embedded word break character in a potential
  693.              match, so we need to prepend a quote character if we
  694.              are replacing the completion string. */
  695.           replacement = xmalloc (strlen (matches[0]) + 2);
  696.           quote_char = *rl_completer_quote_characters;
  697.           *replacement = quote_char;
  698.           strcpy (replacement + 1, matches[0]);
  699. #endif /* SHELL */
  700.         }
  701.         }
  702.  
  703.       if (replacement)
  704.         {
  705.           rl_begin_undo_group ();
  706.           rl_delete_text (start, rl_point);
  707.           rl_point = start;
  708.           rl_insert_text (replacement);
  709.           rl_end_undo_group ();
  710.           if (replacement != matches[0])
  711.         free (replacement);
  712.         }
  713.  
  714.       /* If there are more matches, ring the bell to indicate.
  715.          If this was the only match, and we are hacking files,
  716.          check the file to see if it was a directory.  If so,
  717.          add a '/' to the name.  If not, and we are at the end
  718.          of the line, then add a space. */
  719.       if (matches[1])
  720.         {
  721.           if (what_to_do == '!')
  722.         goto display_matches;        /* XXX */
  723.           else if (rl_editing_mode != vi_mode)
  724.         ding ();    /* There are other matches remaining. */
  725.         }
  726.       else
  727.         {
  728.           char temp_string[4];
  729.           int temp_string_index = 0;
  730.  
  731.           if (quote_char)
  732.         temp_string[temp_string_index++] = quote_char;
  733.  
  734.           temp_string[temp_string_index++] = delimiter ? delimiter : ' ';
  735.           temp_string[temp_string_index++] = '\0';
  736.  
  737.           if (rl_filename_completion_desired)
  738.         {
  739.           struct stat finfo;
  740.           char *filename = tilde_expand (matches[0]);
  741.  
  742.           if ((stat (filename, &finfo) == 0) && S_ISDIR (finfo.st_mode))
  743.             {
  744.               if (rl_line_buffer[rl_point] != '/')
  745.             rl_insert_text ("/");
  746.             }
  747.           else
  748.             {
  749.               if (rl_point == rl_end)
  750.             rl_insert_text (temp_string);
  751.             }
  752.           free (filename);
  753.         }
  754.           else
  755.         {
  756.           if (rl_point == rl_end)
  757.             rl_insert_text (temp_string);
  758.         }
  759.         }
  760.       break;
  761.  
  762.     case '*':
  763.       {
  764.         int i = 1;
  765.  
  766.         rl_begin_undo_group ();
  767.         rl_delete_text (start, rl_point);
  768.         rl_point = start;
  769.         if (matches[1])
  770.           {
  771.         while (matches[i])
  772.           {
  773.             rl_insert_text (matches[i++]);
  774.             rl_insert_text (" ");
  775.           }
  776.           }
  777.         else
  778.           {
  779.         rl_insert_text (matches[0]);
  780.         rl_insert_text (" ");
  781.           }
  782.         rl_end_undo_group ();
  783.       }
  784.       break;
  785.  
  786.     case '?':
  787.       {
  788.         int len, count, limit, max;
  789.         int j, k, l;
  790.  
  791.         /* Handle simple case first.  What if there is only one answer? */
  792.         if (!matches[1])
  793.           {
  794.         char *temp;
  795.  
  796.         temp = printable_part (matches[0]);
  797.         crlf ();
  798.         print_filename (temp, matches[0]);
  799.         crlf ();
  800.         goto restart;
  801.           }
  802.  
  803.         /* There is more than one answer.  Find out how many there are,
  804.            and find out what the maximum printed length of a single entry
  805.            is. */
  806.       display_matches:
  807.         for (max = 0, i = 1; matches[i]; i++)
  808.           {
  809.         char *temp;
  810.         int name_length;
  811.  
  812.         temp = printable_part (matches[i]);
  813.         name_length = strlen (temp);
  814.  
  815.         if (name_length > max)
  816.           max = name_length;
  817.           }
  818.  
  819.         len = i - 1;
  820.  
  821.         /* If there are many items, then ask the user if she
  822.            really wants to see them all. */
  823.         if (len >= rl_completion_query_items)
  824.           {
  825.         crlf ();
  826.         fprintf (rl_outstream,
  827.              "There are %d possibilities.  Do you really", len);
  828.         crlf ();
  829.         fprintf (rl_outstream, "wish to see them all? (y or n)");
  830.         fflush (rl_outstream);
  831.         if (!get_y_or_n ())
  832.           {
  833.             crlf ();
  834.             goto restart;
  835.           }
  836.           }
  837.  
  838.         /* How many items of MAX length can we fit in the screen window? */
  839.         max += 2;
  840.         limit = screenwidth / max;
  841.         if (limit != 1 && (limit * max == screenwidth))
  842.           limit--;
  843.  
  844.         /* Avoid a possible floating exception.  If max > screenwidth,
  845.            limit will be 0 and a divide-by-zero fault will result. */
  846.         if (limit == 0)
  847.           limit = 1;
  848.  
  849.         /* How many iterations of the printing loop? */
  850.         count = (len + (limit - 1)) / limit;
  851.  
  852.         /* Watch out for special case.  If LEN is less than LIMIT, then
  853.            just do the inner printing loop. */
  854.         if (len < limit)
  855.           count = 1;
  856.  
  857.         /* Sort the items if they are not already sorted. */
  858.         if (!rl_ignore_completion_duplicates)
  859.           qsort (matches, len, sizeof (char *), compare_strings);
  860.  
  861.         /* Print the sorted items, up-and-down alphabetically, like
  862.            ls might. */
  863.         crlf ();
  864.  
  865.         for (i = 1; i < count + 1; i++)
  866.           {
  867.         for (j = 0, l = i; j < limit; j++)
  868.           {
  869.             if (l > len || !matches[l])
  870.               break;
  871.             else
  872.               {
  873.             char *temp;
  874.             int printed_length;
  875.  
  876.             temp = printable_part (matches[l]);
  877.             printed_length = strlen (temp);
  878.             printed_length += print_filename (temp, matches[l]);
  879.  
  880.             if (j + 1 < limit)
  881.               {
  882.                 for (k = 0; k < max - printed_length; k++)
  883.                   putc (' ', rl_outstream);
  884.               }
  885.               }
  886.             l += count;
  887.           }
  888.         crlf ();
  889.           }
  890.       restart:
  891.  
  892.         rl_on_new_line ();
  893.       }
  894.       break;
  895.  
  896.     default:
  897.       fprintf (stderr, "\r\nreadline: bad value for what_to_do in rl_complete\n");
  898.       abort ();
  899.     }
  900.  
  901.       for (i = 0; matches[i]; i++)
  902.     free (matches[i]);
  903.       free (matches);
  904.     }
  905.  
  906.   /* Check to see if the line has changed through all of this manipulation. */
  907.   if (saved_line_buffer)
  908.     {
  909.       if (strcmp (rl_line_buffer, saved_line_buffer) != 0)
  910.     completion_changed_buffer = 1;
  911.       else
  912.     completion_changed_buffer = 0;
  913.  
  914.       free (saved_line_buffer);
  915.     }
  916.   return 0;
  917. }
  918.  
  919. #if defined (VISIBLE_STATS)
  920. /* Return the character which best describes FILENAME.
  921.      `@' for symbolic links
  922.      `/' for directories
  923.      `*' for executables
  924.      `=' for sockets */
  925. static int
  926. stat_char (filename)
  927.      char *filename;
  928. {
  929.   struct stat finfo;
  930.   int character, r;
  931.  
  932. #if defined (S_ISLNK)
  933.   r = lstat (filename, &finfo);
  934. #else
  935.   r = stat (filename, &finfo);
  936. #endif
  937.  
  938.   if (r == -1)
  939.     return (0);
  940.  
  941.   character = 0;
  942.   if (S_ISDIR (finfo.st_mode))
  943.     character = '/';
  944. #if defined (S_ISLNK)
  945.   else if (S_ISLNK (finfo.st_mode))
  946.     character = '@';
  947. #endif /* S_ISLNK */
  948. #if defined (S_ISSOCK)
  949.   else if (S_ISSOCK (finfo.st_mode))
  950.     character = '=';
  951. #endif /* S_ISSOCK */
  952.   else if (S_ISREG (finfo.st_mode))
  953.     {
  954.       if (access (filename, X_OK) == 0)
  955.     character = '*';
  956.     }
  957.   return (character);
  958. }
  959. #endif /* VISIBLE_STATS */
  960.  
  961. /* Stupid comparison routine for qsort () ing strings. */
  962. static int
  963. compare_strings (s1, s2)
  964.   char **s1, **s2;
  965. {
  966.   int result;
  967.  
  968.   result = **s1 - **s2;
  969.   if (result == 0)
  970.     result = strcmp (*s1, *s2);
  971.  
  972.   return result;
  973. }
  974.  
  975. /* A completion function for usernames.
  976.    TEXT contains a partial username preceded by a random
  977.    character (usually `~').  */
  978. char *
  979. username_completion_function (text, state)
  980.      int state;
  981.      char *text;
  982. {
  983. #if defined (__GO32__)
  984.   return (char *)NULL;
  985. #else /* !__GO32__ */
  986.   static char *username = (char *)NULL;
  987.   static struct passwd *entry;
  988.   static int namelen, first_char, first_char_loc;
  989.  
  990.   if (!state)
  991.     {
  992.       if (username)
  993.     free (username);
  994.  
  995.       first_char = *text;
  996.  
  997.       if (first_char == '~')
  998.     first_char_loc = 1;
  999.       else
  1000.     first_char_loc = 0;
  1001.  
  1002.       username = savestring (&text[first_char_loc]);
  1003.       namelen = strlen (username);
  1004.       setpwent ();
  1005.     }
  1006.  
  1007.   while (entry = getpwent ())
  1008.     {
  1009.       if ((username[0] == entry->pw_name[0]) &&
  1010.       (strncmp (username, entry->pw_name, namelen) == 0))
  1011.     break;
  1012.     }
  1013.  
  1014.   if (!entry)
  1015.     {
  1016.       endpwent ();
  1017.       return ((char *)NULL);
  1018.     }
  1019.   else
  1020.     {
  1021.       char *value = xmalloc (2 + strlen (entry->pw_name));
  1022.  
  1023.       *value = *text;
  1024.  
  1025.       strcpy (value + first_char_loc, entry->pw_name);
  1026.  
  1027.       if (first_char == '~')
  1028.     rl_filename_completion_desired = 1;
  1029.  
  1030.       return (value);
  1031.     }
  1032. #endif /* !__GO32__ */
  1033. }
  1034.  
  1035. /* **************************************************************** */
  1036. /*                                    */
  1037. /*                 Completion                    */
  1038. /*                                    */
  1039. /* **************************************************************** */
  1040.  
  1041. /* Non-zero means that case is not significant in completion. */
  1042. int completion_case_fold = 0;
  1043.  
  1044. /* Return an array of (char *) which is a list of completions for TEXT.
  1045.    If there are no completions, return a NULL pointer.
  1046.    The first entry in the returned array is the substitution for TEXT.
  1047.    The remaining entries are the possible completions.
  1048.    The array is terminated with a NULL pointer.
  1049.  
  1050.    ENTRY_FUNCTION is a function of two args, and returns a (char *).
  1051.      The first argument is TEXT.
  1052.      The second is a state argument; it should be zero on the first call, and
  1053.      non-zero on subsequent calls.  It returns a NULL pointer to the caller
  1054.      when there are no more matches.
  1055.  */
  1056. char **
  1057. completion_matches (text, entry_function)
  1058.      char *text;
  1059.      CPFunction *entry_function;
  1060. {
  1061.   /* Number of slots in match_list. */
  1062.   int match_list_size;
  1063.  
  1064.   /* The list of matches. */
  1065.   char **match_list =
  1066.     (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
  1067.  
  1068.   /* Number of matches actually found. */
  1069.   int matches = 0;
  1070.  
  1071.   /* Temporary string binder. */
  1072.   char *string;
  1073.  
  1074.   match_list[1] = (char *)NULL;
  1075.  
  1076.   while (string = (*entry_function) (text, matches))
  1077.     {
  1078.       if (matches + 1 == match_list_size)
  1079.     match_list = (char **)xrealloc
  1080.       (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
  1081.  
  1082.       match_list[++matches] = string;
  1083.       match_list[matches + 1] = (char *)NULL;
  1084.     }
  1085.  
  1086.   /* If there were any matches, then look through them finding out the
  1087.      lowest common denominator.  That then becomes match_list[0]. */
  1088.   if (matches)
  1089.     {
  1090.       register int i = 1;
  1091.       int low = 100000;        /* Count of max-matched characters. */
  1092.  
  1093.       /* If only one match, just use that. */
  1094.       if (matches == 1)
  1095.     {
  1096.       match_list[0] = match_list[1];
  1097.       match_list[1] = (char *)NULL;
  1098.     }
  1099.       else
  1100.     {
  1101.       /* Otherwise, compare each member of the list with
  1102.          the next, finding out where they stop matching. */
  1103.  
  1104.       while (i < matches)
  1105.         {
  1106.           register int c1, c2, si;
  1107.  
  1108.           if (completion_case_fold)
  1109.         {
  1110.           for (si = 0;
  1111.                (c1 = to_lower(match_list[i][si])) &&
  1112.                (c2 = to_lower(match_list[i + 1][si]));
  1113.                si++)
  1114.             if (c1 != c2) break;
  1115.         }
  1116.           else
  1117.         {
  1118.           for (si = 0;
  1119.                (c1 = match_list[i][si]) &&
  1120.                (c2 = match_list[i + 1][si]);
  1121.                si++)
  1122.             if (c1 != c2) break;
  1123.         }
  1124.  
  1125.           if (low > si) low = si;
  1126.           i++;
  1127.         }
  1128.       match_list[0] = xmalloc (low + 1);
  1129.       strncpy (match_list[0], match_list[1], low);
  1130.       match_list[0][low] = '\0';
  1131.     }
  1132.     }
  1133.   else                /* There were no matches. */
  1134.     {
  1135.       free (match_list);
  1136.       match_list = (char **)NULL;
  1137.     }
  1138.   return (match_list);
  1139. }
  1140.  
  1141. /* Okay, now we write the entry_function for filename completion.  In the
  1142.    general case.  Note that completion in the shell is a little different
  1143.    because of all the pathnames that must be followed when looking up the
  1144.    completion for a command. */
  1145. char *
  1146. filename_completion_function (text, state)
  1147.      int state;
  1148.      char *text;
  1149. {
  1150.   static DIR *directory;
  1151.   static char *filename = (char *)NULL;
  1152.   static char *dirname = (char *)NULL;
  1153.   static char *users_dirname = (char *)NULL;
  1154.   static int filename_len;
  1155.  
  1156.   struct dirent *entry = (struct dirent *)NULL;
  1157.  
  1158.   /* If we don't have any state, then do some initialization. */
  1159.   if (!state)
  1160.     {
  1161.       char *temp;
  1162.  
  1163.       if (dirname) free (dirname);
  1164.       if (filename) free (filename);
  1165.       if (users_dirname) free (users_dirname);
  1166.  
  1167.       filename = savestring (text);
  1168.       if (!*text) text = ".";
  1169.       dirname = savestring (text);
  1170.  
  1171.       temp = strrchr (dirname, '/');
  1172.  
  1173.       if (temp)
  1174.     {
  1175.       strcpy (filename, ++temp);
  1176.       *temp = '\0';
  1177.     }
  1178.       else
  1179.     strcpy (dirname, ".");
  1180.  
  1181.       /* We aren't done yet.  We also support the "~user" syntax. */
  1182.  
  1183.       /* Save the version of the directory that the user typed. */
  1184.       users_dirname = savestring (dirname);
  1185.       {
  1186.     char *temp_dirname;
  1187.     int replace_dirname;
  1188.  
  1189.     temp_dirname = tilde_expand (dirname);
  1190.     free (dirname);
  1191.     dirname = temp_dirname;
  1192.  
  1193.     replace_dirname = 0;
  1194.     if (rl_directory_completion_hook)
  1195.       replace_dirname = (*rl_directory_completion_hook) (&dirname);
  1196.     if (replace_dirname)
  1197.       {
  1198.         free (users_dirname);
  1199.         users_dirname = savestring (dirname);
  1200.       }
  1201.       }
  1202.       directory = opendir (dirname);
  1203.       filename_len = strlen (filename);
  1204.  
  1205.       rl_filename_completion_desired = 1;
  1206.     }
  1207.  
  1208.   /* At this point we should entertain the possibility of hacking wildcarded
  1209.      filenames, like /usr/man/man<WILD>/te<TAB>.  If the directory name
  1210.      contains globbing characters, then build an array of directories, and
  1211.      then map over that list while completing. */
  1212.   /* *** UNIMPLEMENTED *** */
  1213.  
  1214.   /* Now that we have some state, we can read the directory. */
  1215.  
  1216.   while (directory && (entry = readdir (directory)))
  1217.     {
  1218.       /* Special case for no filename.
  1219.      All entries except "." and ".." match. */
  1220.       if (!filename_len)
  1221.     {
  1222.       if ((strcmp (entry->d_name, ".") != 0) &&
  1223.           (strcmp (entry->d_name, "..") != 0))
  1224.         break;
  1225.     }
  1226.       else
  1227.     {
  1228.       /* Otherwise, if these match up to the length of filename, then
  1229.          it is a match. */
  1230.         if ((entry->d_name[0] == filename[0]) &&
  1231.         (((int)D_NAMLEN (entry)) >= filename_len) &&
  1232.         (strncmp (filename, entry->d_name, filename_len) == 0))
  1233.           break;
  1234.     }
  1235.     }
  1236.  
  1237.   if (!entry)
  1238.     {
  1239.       if (directory)
  1240.     {
  1241.       closedir (directory);
  1242.       directory = (DIR *)NULL;
  1243.     }
  1244.       if (dirname)
  1245.     {
  1246.       free (dirname);
  1247.       dirname = (char *)NULL;
  1248.     }
  1249.       if (filename)
  1250.     {
  1251.       free (filename);
  1252.       filename = (char *)NULL;
  1253.     }
  1254.       if (users_dirname)
  1255.     {
  1256.       free (users_dirname);
  1257.       users_dirname = (char *)NULL;
  1258.     }
  1259.  
  1260.       return (char *)NULL;
  1261.     }
  1262.   else
  1263.     {
  1264.       char *temp;
  1265.  
  1266.       /* dirname && (strcmp (dirname, ".") != 0) */
  1267.       if (dirname && (dirname[0] != '.' || dirname[1]))
  1268.     {
  1269.       if (rl_complete_with_tilde_expansion && *users_dirname == '~')
  1270.         {
  1271.           int dirlen = strlen (dirname);
  1272.           temp = xmalloc (2 + dirlen + D_NAMLEN (entry));
  1273.           strcpy (temp, dirname);
  1274.           /* Canonicalization cuts off any final slash present.  We need
  1275.          to add it back. */
  1276.           if (dirname[dirlen - 1] != '/')
  1277.             {
  1278.               temp[dirlen] = '/';
  1279.               temp[dirlen + 1] = '\0';
  1280.             }
  1281.         }
  1282.       else
  1283.         {
  1284.           temp = xmalloc (1 + strlen (users_dirname) + D_NAMLEN (entry));
  1285.           strcpy (temp, users_dirname);
  1286.         }
  1287.  
  1288.       strcat (temp, entry->d_name);
  1289.     }
  1290.       else
  1291.     temp = (savestring (entry->d_name));
  1292.  
  1293.       return (temp);
  1294.     }
  1295. }
  1296.  
  1297. /* A function for simple tilde expansion. */
  1298. int
  1299. rl_tilde_expand (ignore, key)
  1300.      int ignore, key;
  1301. {
  1302.   register int start, end;
  1303.   char *homedir;
  1304.  
  1305.   end = rl_point;
  1306.   start = end - 1;
  1307.  
  1308.   if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
  1309.     {
  1310.       homedir = tilde_expand ("~");
  1311.       goto insert;
  1312.     }
  1313.   else if (rl_line_buffer[start] != '~')
  1314.     {
  1315.       for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--);
  1316.       start++;
  1317.     }
  1318.  
  1319.   end = start;
  1320.   do
  1321.     {
  1322.       end++;
  1323.     }
  1324.   while (!whitespace (rl_line_buffer[end]) && end < rl_end);
  1325.  
  1326.   if (whitespace (rl_line_buffer[end]) || end >= rl_end)
  1327.     end--;
  1328.  
  1329.   /* If the first character of the current word is a tilde, perform
  1330.      tilde expansion and insert the result.  If not a tilde, do
  1331.      nothing. */
  1332.   if (rl_line_buffer[start] == '~')
  1333.     {
  1334.       char *temp;
  1335.       int len;
  1336.  
  1337.       len = end - start + 1;
  1338.       temp = xmalloc (len + 1);
  1339.       strncpy (temp, rl_line_buffer + start, len);
  1340.       temp[len] = '\0';
  1341.       homedir = tilde_expand (temp);
  1342.       free (temp);
  1343.  
  1344.     insert:
  1345.       rl_begin_undo_group ();
  1346.       rl_delete_text (start, end + 1);
  1347.       rl_point = start;
  1348.       rl_insert_text (homedir);
  1349.       rl_end_undo_group ();
  1350.     }
  1351.  
  1352.   return (0);
  1353. }
  1354.  
  1355. /* Find the first occurrence in STRING1 of any character from STRING2.
  1356.    Return a pointer to the character in STRING1. */
  1357. static char *
  1358. rl_strpbrk (string1, string2)
  1359.      char *string1, *string2;
  1360. {
  1361.   register char *scan;
  1362.  
  1363.   for (; *string1; string1++)
  1364.     {
  1365.       for (scan = string2; *scan; scan++)
  1366.     {
  1367.       if (*string1 == *scan)
  1368.         {
  1369.           return (string1);
  1370.         }
  1371.     }
  1372.     }
  1373.   return ((char *)NULL);
  1374. }
  1375.  
  1376. #if defined (STATIC_MALLOC)
  1377.  
  1378. /* **************************************************************** */
  1379. /*                                    */
  1380. /*            xmalloc and xrealloc ()                     */
  1381. /*                                    */
  1382. /* **************************************************************** */
  1383.  
  1384. static void memory_error_and_abort ();
  1385.  
  1386. static char *
  1387. xmalloc (bytes)
  1388.      int bytes;
  1389. {
  1390.   char *temp = (char *)malloc (bytes);
  1391.  
  1392.   if (!temp)
  1393.     memory_error_and_abort ();
  1394.   return (temp);
  1395. }
  1396.  
  1397. static char *
  1398. xrealloc (pointer, bytes)
  1399.      char *pointer;
  1400.      int bytes;
  1401. {
  1402.   char *temp;
  1403.  
  1404.   if (!pointer)
  1405.     temp = (char *)malloc (bytes);
  1406.   else
  1407.     temp = (char *)realloc (pointer, bytes);
  1408.  
  1409.   if (!temp)
  1410.     memory_error_and_abort ();
  1411.  
  1412.   return (temp);
  1413. }
  1414.  
  1415. static void
  1416. memory_error_and_abort ()
  1417. {
  1418.   fprintf (stderr, "readline: Out of virtual memory!\n");
  1419.   abort ();
  1420. }
  1421. #endif /* STATIC_MALLOC */
  1422.